package 内存可见性;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:这里加上了sleep,1秒执行一次while循环,1秒足够在内存中读取数据,可以认为没有被优化了
 * User: lenovo
 * Date: 2022-09-24
 * Time: 21:48
 */
public class Demo2 {
    static class Counter{
        public int count;

    }
    public static void main(String[] args) {
        Counter counter = new Counter();
        Thread t1 = new Thread(){
            @Override
            public void run() {
                while (counter.count == 0){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("t1运行结束");
            }
        };
        t1.start();

        Thread t2 = new Thread(){
            @Override
            public void run() {
                System.out.println("请输入数字:");
                Scanner scanner = new Scanner(System.in);
                counter.count = scanner.nextInt();
            }
        };
        t2.start();
    }
}
